package com.nobrain.uboard.DataManager;

import java.util.ArrayList;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.FacebookError;
import com.facebook.android.Facebook.DialogListener;
import com.nobrain.uboard.Data.FBBaseFeed;
import com.nobrain.uboard.Interfaces.SNSUser;
import com.nobrain.uboard.Util.PreferenceUtil;

public class FacebookInfo implements SNSUser {
	private final static String API_KEY = "177583735658284";
	private Facebook mFacebook;
	private static FacebookInfo info;
	private AsyncTask<Void, Void, Void> task;

	public static FacebookInfo getInstance(){
		if(info == null){
			info = new FacebookInfo();
			info.mFacebook = new Facebook(API_KEY);
		}
		return info;
	}

	@Override
	public void author(final Activity mActivity) {
		mFacebook.authorize2(mActivity, new String[]{"email","publish_stream","offline_access","read_stream"}, new DialogListener() {

			@Override
			public void onFacebookError(FacebookError e) {}

			@Override
			public void onError(DialogError e) {}

			@Override
			public void onComplete(Bundle values) {
				Log.d("INFO", "Connect Success");
				PreferenceUtil.putFaceBookToken(mActivity, mFacebook.getAccessToken());
				updateState(0);
			}

			@Override
			public void onCancel() {}
		});
	}
	
	@Override
	public void updateState(int from) {
		new Thread(new Runnable() {

			@Override
			public void run() {
				if(mFacebook != null){
					try{
						String result = mFacebook.request("me/home");
						Log.d("Parsing", result);
						ArrayList<FBBaseFeed> feeds = ParsingUtil.FacebookData(result);
					}catch (Exception e){
						e.printStackTrace();
					}
				}
			}
		}).start();
	}


}
